package com.langhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.langhua.common.R;
import com.langhua.entity.Foodstuff;
import com.langhua.service.FoodstuffService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/foodstuffs")
@Slf4j
public class FoodstuffController {

    @Autowired
    private FoodstuffService foodstuffService;

    /**
     * 分页查询
     * @param current  当前页
     * @param size     每页条数
     * @param foodstuff   条件查询
     * @return
     */
    @GetMapping("{current}/{size}")
    public R<IPage> getPage(@PathVariable Integer current, @PathVariable Integer size, Foodstuff foodstuff){
        IPage page=new Page(current,size);
        LambdaQueryWrapper<Foodstuff> lqw=new LambdaQueryWrapper<Foodstuff>();

        lqw.like(Strings.isNotEmpty(foodstuff.getName()),Foodstuff::getName,foodstuff.getName());

        IPage page1 = foodstuffService.page(page,lqw);
        return R.success(page1,"foodstuff分页查询");
    }

    /**
     * 分页查询
     * @param foodstuff   条件查询
     * @return
     */
//    @GetMapping("/getAll")
//    public R< List<Foodstuff>> getAll(Foodstuff foodstuff){
//
//        LambdaQueryWrapper<Foodstuff> lqw=new LambdaQueryWrapper<Foodstuff>();
//        lqw.eq(Strings.isNotEmpty(foodstuff.getFlavorType()),Foodstuff::getFlavorType,foodstuff.getFlavorType());
//
//        List<Foodstuff> list = foodstuffService.list(lqw);
//        return R.success(list,"foodstuff查询所有");
//    }



    /**
     * 查询所有食材类型(去重)
     * @return
     */
    @GetMapping
    public R<List<Foodstuff>> getPage(){

        QueryWrapper<Foodstuff> foodstuffQueryWrapper = new QueryWrapper<>();
        foodstuffQueryWrapper.select("DISTINCT type");
        List<Foodstuff> list = foodstuffService.list(foodstuffQueryWrapper);
        System.out.println("foodstuff查询所有食材类型(去重)："+list);
        return R.success(list,"foodstuff查询所有食材类型(去重)");
    }
    /**
     * 查询 根据类型查询食材
     * @return
     */
//    @GetMapping("/getMenuByFlavorType")
//    public R<List<Foodstuff>> getMenuByFlavorType(@RequestParam String flavortype){
//        LambdaQueryWrapper<Foodstuff> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Foodstuff::getFlavorType,flavortype);
//        List<Foodstuff> list = foodstuffService.list(lqw);
//
//        System.out.println("foodstuff根据类型查询食材："+list);
//        return R.success(list,"foodstuff根据类型查询食材");
//    }

    /**
     * 新增食材
     * @param foodstuff
     * @return
     */
    @PostMapping("/add")
    public R<String> save(HttpServletRequest request, @RequestBody Foodstuff foodstuff){
        log.info("新增食材，食材信息：{}",foodstuff.toString());
        System.out.println("新增得id："+foodstuff);
        foodstuffService.save(foodstuff);

        return R.success("新增食材成功",null);
    }

    /**
     * 根据id修改食材信息()
     * @param foodstuff
     * @return
     */
    @PutMapping("/updateFoodstuff")
    public R<String> updateFoodstuff(@RequestBody Foodstuff foodstuff){

        foodstuffService.updateById(foodstuff);

        return R.success("食材信息修改成功",null);
    }

    /**
     * 通过id删除Foodstuff
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R<String> deleteById(@PathVariable Long id){
        log.info("删除Foodstuff的id为：{}", id);

        foodstuffService.removeById(id);
        return R.success(null,"删除成功！");
    }
}
